//
//  ZQAFHelper.m
//  jianzhik
//
//  Created by huami on 2018/9/2.
//  Copyright © 2018年 WKWebView. All rights reserved.
//

#import "ZQAFHelper.h"

@implementation ZQAFHelper
//NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
//parameters[@"Username"] = self.userTextField.text;
//parameters[@"Password"] = self.psdTextField.text;

+ (void)afnetworkRequestWithPostURL:(NSString *)url parameters:(NSDictionary *)dict view:(UIView *)view finishedBlcok:(void (^)(NSDictionary *dict))finishedBlcok
{
    
    
    url = [NSString stringWithFormat:@"%@%@",kRequestURL,url];
    [UIHelper showMessage:@"正在加载数据..." toView:view];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:[ZQDataManger shareIntance].loginModels.Token forHTTPHeaderField:@"Authorization"];

    [manager POST:url parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * responseObject) {
    
        if ([responseObject[@"Code"] integerValue] == 0) {
            finishedBlcok(responseObject);
        } else if ([responseObject[@"Code"] integerValue] == 103) {
             [self changeDataQueryData];
        } else {
            [UIHelper showErrorWithText:responseObject[@"Msg"] inView:view];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [UIHelper showErrorWithText:@"网络连接失败" inView:view];
    }];
}

+ (void)afnetworkRequestWithGetURL:(NSString *)url parameters:(NSDictionary *)dict view:(UIView *)view finishedBlcok:(void (^)(NSDictionary *dict))finishedBlcok
{
    url = [NSString stringWithFormat:@"%@%@",kRequestURL,url];

    [UIHelper showMessage:@"正在加载数据..." toView:view];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:[ZQDataManger shareIntance].loginModels.Token forHTTPHeaderField:@"Authorization"];

    [manager GET:url parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * responseObject) {
        NSInteger code = [responseObject[@"Code"] integerValue];
        
        if (code == 0) {
            finishedBlcok(responseObject);
        } else if (code == 103) {
            [self changeDataQueryData];
         
        } else {
            [UIHelper showErrorWithText:responseObject[@"Msg"] inView:view];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [UIHelper showErrorWithText:@"网络连接失败" inView:view];

    }];
}
        
        
+ (void)changeDataQueryData

{
    NSLog(@"改数据");
    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm beginWriteTransaction];
    [ZQSaveModels createOrUpdateInRealm:realm withValue:@{@"Username":[ZQDataManger shareIntance].loginModels.Username,@"Token": @""}];
    // book 对象的 `title` 属性仍旧保持不变
    [realm commitWriteTransaction];
    SendNotify(kTokenFailedNotification, nil, nil);
}


@end
